package cn.xgj.sfzy.service.impl;

import cn.xgj.sfzy.entity.Charge;
import cn.xgj.sfzy.entity.Consult;
import cn.xgj.sfzy.entity.ConsultLv;
import cn.xgj.sfzy.entity.Field;
import cn.xgj.sfzy.mapper.ChargeMapper;
import cn.xgj.sfzy.mapper.ConsultLvMapper;
import cn.xgj.sfzy.mapper.ConsultMapper;
import cn.xgj.sfzy.mapper.FieldMapper;
import cn.xgj.sfzy.service.ConsultLvService;
import cn.xgj.sfzy.service.ConsultService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.activation.FileTypeMap;
import java.util.List;

/**
 * @Author 张卫姣
 * @Date 2024年10月24日 18:02
 * @Version 1.0
 */
@Service
public class ConsultServiceImpl implements ConsultService {

    @Autowired
    private ConsultMapper consultMapper;

    //咨询师等级对象
    @Autowired
    private ConsultLvMapper consultLvMapper;
    //擅长领域对象
    @Autowired
    private FieldMapper fieldMapper;

    //收费标准对象
    @Autowired
    private ChargeMapper chargeMapper;


    @Override
    public Integer add(Consult consult) {
        return consultMapper.insert(consult);
    }

    @Override
    public Integer remove(Long consultId) {
        List<Charge> charges = chargeMapper.selectByChargeId(consultId.intValue());
        if (charges.size()!=0){
            return -1;
        }else{
            return consultMapper.delete(consultId);
        }

    }

    @Override
    public Integer edit(Consult consult) {
        return consultMapper.update(consult);
    }

    @Override
    public List<Consult> findAll(Consult consult) {
        return consultMapper.selectAll(consult);
    }

    @Override
    public Consult findById(Integer consultId) {
        Consult consult = consultMapper.selectById(consultId);
        Integer consultLvId = consult.getConsultLvId();
        ConsultLv consultLv = consultLvMapper.selectById(consultLvId);
        consult.setConsultLv(consultLv);

        //根据id获取擅长领域对象
        Integer consultFieldId = consult.getConsultFieldId();
        Field field = fieldMapper.selectById(consultFieldId);
        consult.setField(field);

        return consult;
    }

    @Override
    public PageInfo<Consult> findByPage(Integer pageNum, Integer pageSize, Consult consult) {
        PageHelper.startPage(pageNum,pageSize);
        List<Consult> consults = consultMapper.selectAll(consult);
        for (Consult con : consults){
            //根据id获取咨询师等级对象
            Integer consultLvId = con.getConsultLvId();
            ConsultLv consultLv = consultLvMapper.selectById(consultLvId);
            con.setConsultLv(consultLv);

            //根据id获取擅长领域对象
            Integer consultFieldId = con.getConsultFieldId();
            Field field = fieldMapper.selectById(consultFieldId);
            con.setField(field);

        }
        PageInfo<Consult> pageInfo = new PageInfo<Consult>(consults);

        return pageInfo;


    }
}
